<?php
class Items_Model extends CI_Model {
	
	const SCOPE_PUBLIC = 0;		// Item to public
	const SCOPE_INTERNAL = 1;	// Item to internal
	
	/**
	 * Get public items from database.
	 * $min_id and $max_id are the scope for searching.
	 * 
	 * @param int $min_id	/ The minimal itemid
	 * @param int $max_id	/ The maximal itemid
	 * @param int $limit 	/ The max number of items to search
	 * @return array
	 * Array ( 
	 * 	[0] => Array ( [ITEMID] => 2 [USERID] => cjphenry [TITLE] => title2 [CONTENT] => content2 [PICPATHS] => no [SCOPE] => 0 [TOUSERIDS] => no [CREATETIME] => 2011-09-22 16:11:44 ) 
	 * 	[1] => Array ( [ITEMID] => 1 [USERID] => cjphenry [TITLE] => title1 [CONTENT] => content1 [PICPATHS] => no [SCOPE] => 0 [TOUSERIDS] => no [CREATETIME] => 2011-09-22 16:03:49 ) 
	 * )
	 */
	public function getPublicItems($min_id, $max_id, $limit) {
		
		if (! is_int ( $min_id ) || ! is_int ( $max_id ) || ! is_int ( $limit ))
			return false;
		
		$this->load->database ();
		
		$this->db->where ( 'ITEMID >=', $min_id );
		$this->db->where ( 'ITEMID <=', $max_id );
		$this->db->where( 'SCOPE', self::SCOPE_PUBLIC );
		$this->db->order_by ( 'ITEMID', 'DESC' );
		$this->db->limit ( $limit );
		$this->db->from ( 'ITEMS' );
		$query = $this->db->get ();
		
		$this->db->close ();
		
		return $query->result_array ();
	}

	/**
	 * This function have not been completed
	 * 
	 * @param unknown_type $current_num
	 * @param unknown_type $next_num
	 */
	public function getPopularItems(  ) {
		
	}
	
	public function getItemsToUser( $userid, $min_id, $max_id, $limit ) {
		
		if (!is_string( $userid ) || ! is_int ( $min_id ) || ! is_int ( $max_id ) || ! is_int ( $limit ))
			return false;
		
		$this->load->database ();
		
		$this->db->where ( 'ITEMID >=', $min_id );
		$this->db->where ( 'ITEMID <=', $max_id );
		$this->db->where( 'SCOPE', self::SCOPE_INTERNAL );
		$this->db->like( 'TOUSERIDS', '|'.$userid.'|');
		$this->db->order_by ( 'ITEMID', 'DESC' );
		$this->db->limit ( $limit );
		$this->db->from ( 'ITEMS' );
		$query = $this->db->get ();
		
		$this->db->close ();
		
		return $query->result_array ();
	}
	
	public function getItemsByHost($userid, $min_id, $max_id, $limit) {
		
		if (!is_string( $userid ) || ! is_int ( $min_id ) || ! is_int ( $max_id ) || ! is_int ( $limit ))
			return false;
		
		$this->load->database ();
		
		$this->db->where ( 'ITEMID >=', $min_id );
		$this->db->where ( 'ITEMID <=', $max_id );
		$this->db->where ('USERID', $userid);
		$this->db->order_by ( 'ITEMID', 'DESC' );
		$this->db->limit ( $limit );
		$this->db->from ( 'ITEMS' );
		$query = $this->db->get ();
		
		$this->db->close ();
		
		return $query->result_array ();
	}
	
	public function getItemsByGuest($hostid, $guestid, $min_id, $max_id, $limit) {
		
		if (!is_string( $hostid ) || !is_string( $guestid ) || ! is_int ( $min_id ) || ! is_int ( $max_id ) || ! is_int ( $limit ))
			return false;
		
		$this->load->database ();
		
		$this->db->where ( 'ITEMID >=', $min_id );
		$this->db->where ( 'ITEMID <=', $max_id );
		$this->db->where ('USERID', $guestid);
		$this->db->like ('TOUSERIDS', '|'.$hostid.'|' );
		$this->db->order_by ( 'ITEMID', 'DESC' );
		$this->db->limit ( $limit );
		$this->db->from ( 'ITEMS' );
		$query = $this->db->get ();
		
		$this->db->close ();
		
		return $query->result_array ();
	}
	
	public function createItem( $data ) {

		$required = array('USERID', 'TITLE', 'CONTENT', 'PICPATHS', 'SCOPE', 'TOUSERIDS');
		if( ! $this->_required($required, $data))
			return false;
			
		$data['CREATETIME'] = date('Y-m-d H:i:s');
		
		$this->load->database();
		$this->db->insert('ITEMS', $data);
		$id = $this->db->insert_id();
		$this->db->close();
		
		return $id;
	}
	
	public function deleteItem( $item_id ) {
		
		if( !is_int($item_id) )
			return false;
		
		$this->load->database();
		$this->db->delete('ITEMS', array('ITEMID'=>$item_id));
		$this->db->close();
		
		return true;
	}
}